package com.sig.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

/**
 * Http工具
 *
 * @author sig
 * @since 2019/1/26
 */
public final class HttpUtil {

    private HttpUtil() {
    }

    public static String httpGet(final String spec) {
        HttpURLConnection conn = null;
        InputStream in = null;
        BufferedReader bReader = null;
        final StringBuilder builder = new StringBuilder();
        try {
            final URL url = new URL(spec);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Content-Type", "text/plain");
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            conn.connect();
            in = conn.getResponseCode() == 200 ? conn.getInputStream() : conn.getErrorStream();
            bReader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = bReader.readLine()) != null)
                builder.append(new String(line.getBytes(), Charset.forName("UTF-8"))).append("\n");
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            if (bReader != null) {
                try {
                    bReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) conn.disconnect();
        }
        return builder.toString();
    }

    public static String httpPost(final String spec, final String paramStr) {
        HttpURLConnection conn = null;
        InputStream in = null;
        BufferedReader bReader = null;
        final StringBuilder builder = new StringBuilder();
        try {
            final URL url = new URL(spec);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            conn.connect();
            final PrintStream ps = new PrintStream(conn.getOutputStream());
            ps.print(paramStr);
            ps.flush();
            in = conn.getResponseCode() == 200 ? conn.getInputStream() : conn.getErrorStream();
            bReader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = bReader.readLine()) != null)
                builder.append(new String(line.getBytes(), Charset.forName("UTF-8"))).append("\n");
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            if (bReader != null) {
                try {
                    bReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) conn.disconnect();
        }
        return builder.toString();
    }

    public static String httpForm(final String spec, final String paramStr) {
        HttpURLConnection conn = null;
        InputStream in = null;
        BufferedReader bReader = null;
        final StringBuilder builder = new StringBuilder();
        try {
            final URL url = new URL(spec);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            conn.connect();
            final PrintStream ps = new PrintStream(conn.getOutputStream());
            ps.print(paramStr);
            ps.flush();
            in = conn.getResponseCode() == 200 ? conn.getInputStream() : conn.getErrorStream();
            bReader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = bReader.readLine()) != null)
                builder.append(new String(line.getBytes(), Charset.forName("UTF-8"))).append("\n");
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            if (bReader != null) {
                try {
                    bReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) conn.disconnect();
        }
        return builder.toString();
    }
}
